using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Principal;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{

        for (int i = 0; i < SiteMap.RootNode.ChildNodes.Count; i++)
        {
            //GRABS SITEMAP MAIN ITEMS (UNDER HOME)
            SiteMapNode smn = (SiteMapNode)SiteMap.RootNode.ChildNodes[i];

            //CREATES ACCORDION PANE
            AjaxControlToolkit.AccordionPane p = new AjaxControlToolkit.AccordionPane();

            //CREATE UNIQUE PANE ID
            p.ID = "Pane" + i;

            //CREATE HEADER ITEM
            HyperLink hlHeader = new HyperLink();
            hlHeader.NavigateUrl = SiteMap.RootNode.ChildNodes[i].Url.ToString();
            hlHeader.Text = SiteMap.RootNode.ChildNodes[i].Title.ToString();

            //ADDS HEADER LINK TO PANE (HEADER)
            p.HeaderContainer.Controls.Add(hlHeader);

            //CHECKS IF HEADER ITEM HAS CHILDREN
            if (smn.HasChildNodes)
            {
                //CREATE BULLETED LIST OF CHILDREN
                BulletedList blMenu = new BulletedList();

                blMenu.DisplayMode = BulletedListDisplayMode.HyperLink;

                //CREATES LIST ITEMS WITHIN BULLETED LIST FOR CHILDREN
                for (int j = 0; j < smn.ChildNodes.Count; j++)
                {
                    blMenu.Items.Insert(j, (new ListItem(smn.ChildNodes[j].Title.ToString(), smn.ChildNodes[j].Url.ToString())));
                }

                //ADDS BULLETED LIST TO PANE (CONTAINER)
                p.ContentContainer.Controls.Add(blMenu);
            }
            //ADDS PANE TO ACCORDION
            Accordion1.Panes.Add(p);
        }
        
        /*if (Session["nombreUsuario"] == "" || Session["nombreUsuario"] == null)
        {
            //Session["nombreUsuario"] = "marwulo0";
            Accordion1.Enabled = false;
            this.LinkButton1.Visible = false;
            this.LinkButton2.Visible = false;
        }
        else
        {
            Accordion1.Enabled = true;
            
        }*/

        IIdentity identity = HttpContext.Current.User.Identity;

        if (identity.IsAuthenticated)
        {    
            // Find out what role (if any) the user belongs to
            EntidadesTO.UsuarioTO usuarioTO = new EntidadesTO.UsuarioTO();
            usuarioTO.NombreUsuario = identity.Name;

            usuarioTO = Controladoras.UsuarioMantenedor.ObtenerInstancia().ObtenerUsuario(usuarioTO);

            this.Label1.Text = "Usuario: " + usuarioTO.NombreUsuario;
            this.LinkButton1.Visible = true;
            this.LinkButton2.Visible = true;

        }
        else
        {
            this.Label1.Text = "Usuario: No conectado";
            this.LinkButton1.Visible = false;
            this.LinkButton2.Visible = false;
        }
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("../FrmPrincipal.aspx");
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        //Session["nombreUsuario"] = null;
        //Response.Redirect("FrmLogin.aspx");

        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();

    }
}


